package com.canaan.business.service.agv.impl;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.canaan.business.common.enums.agv.AgvEnum;
import com.canaan.business.common.enums.agv.AgvResultCodeEnum;
import com.canaan.business.common.enums.inOutStock.InOrderStatusEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.common.helper.EmptyStockHelper;
import com.canaan.business.domain.agv.AgvRcsRecord;
import com.canaan.business.domain.agv.AgvTaskConfig;
import com.canaan.business.domain.agv.AgvTaskDispatch;
import com.canaan.business.domain.agv.AgvTaskDispatchRecord;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.inOutStock.AdjustLocationBill;
import com.canaan.business.domain.inOutStock.InStockBill;
import com.canaan.business.domain.inOutStock.OutStockBill;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.*;
import com.canaan.business.mapper.agv.AgvTaskDispatchMapper;
import com.canaan.business.service.agv.IAgvRcsRecordService;
import com.canaan.business.service.agv.IAgvTaskConfigService;
import com.canaan.business.service.agv.IAgvTaskDispatchRecordService;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.business.service.basic.IWmsStorehouseLocationService;
import com.canaan.business.service.inOutStock.IAdjustLocationBillService;
import com.canaan.business.service.inOutStock.IInStockBillService;
import com.canaan.business.service.inOutStock.IOutStockBillService;
import com.canaan.business.service.inOutStock.IWmsInStockOrderService;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.utils.StringUtils;
import com.canaan.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.canaan.common.core.domain.R;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
/**
 * AGV任务调度Service业务层处理
 *
 */
@Slf4j
@Service
public class AgvTaskDispatchServiceImpl implements IAgvTaskDispatchService {

    @Resource
    private AgvTaskDispatchMapper agvTaskDispatchMapper;
    @Resource
    private IAdjustLocationBillService adjustLocationBillService;

    /**
     * 生成任务单
     */
    private final String SCHEDULING_TASK = "/Task/CreateTask";

    /**
     * 生成任务单
     */
    private final String SCHEDULING_TASK_LIST = "/Task/CreateTaskList";

    /**
     * agv暂停
     */
    private final String CONTINUE_AGV = "/stopRobot";
    /**
     * 取消任务
     */
    private final String CANCEL_TASK = "/Task/StopAgvTask";
    /**
     * 继续任务
     */
    private final String CONTINUE_TASK = "/resumeRobot";

    /**
     * 继续任务
     */
    private final String CONTINUE_NEXT = "/continueTask";
    @Value("${agv.flag}")
    private boolean flag;
    @Value("${agv.task.url}")
    private String agvUrl;
    @Value("${agv.position.shelves}")
    private String pointShelves;
    @Value("${agv.task.sysToken}")
    private String sysToken;
    @Value("${agv.task.mapCode}")
    private String mapCode;

    @Resource
    private IAgvRcsRecordService rcsRecordService;

    @Autowired
    private IAgvTaskDispatchService taskDispatchService;

    @Resource
    private IAgvTaskDispatchRecordService taskDispatchRecordService;

    @Resource
    private IWmsStorehouseStockService wmsStorehouseStockService;
    @Resource
    private EmptyStockHelper emptyStockHelper;
    @Resource
    private IAgvTaskConfigService agvTaskConfigService;

    @Resource
    private ISysConfigService sysconfig;

    @Resource
    private IWmsStorehouseLocationService locationService;
    @Resource
    private IInStockBillService inStockBillService;
    @Resource
    private IOutStockBillService outStockBillService;
    /**
     * 查询AGV任务调度
     *
     * @param id AGV任务调度主键
     * @return AGV任务调度
     */
    @Override
    public AgvTaskDispatch selectAgvTaskDispatchById(Long id) {
        return agvTaskDispatchMapper.selectAgvTaskDispatchById(id);
    }

    /**
     * 查询AGV任务调度列表
     *
     * @param agvTaskDispatch AGV任务调度
     * @return AGV任务调度
     */
    @Override
    public List<AgvTaskDispatch> selectAgvTaskDispatchListOrderByIdDesc(AgvTaskDispatch agvTaskDispatch) {
        return agvTaskDispatchMapper.selectAgvTaskDispatchListOrderByIdDesc(agvTaskDispatch);
    }

    /**
     * 查询AGV任务调度列表
     *
     * @param agvTaskDispatch AGV任务调度
     * @return AGV任务调度
     */
    @Override
    public List<AgvTaskDispatch> selectAgvTaskDispatchList(AgvTaskDispatch agvTaskDispatch) {
        return agvTaskDispatchMapper.selectAgvTaskDispatchList(agvTaskDispatch);
    }

    @Override
    public List<AgvTaskDispatch> selectAgvTaskDispatchListByStartPoint(String startPoint) {
        return agvTaskDispatchMapper.selectAgvTaskDispatchListByStartPoint(startPoint);
    }

    /**
     * 查询AGV任务调度列表
     *
     * @param endPoint AGV任务调度
     * @return AGV任务调度
     */
    @Override
    public List<AgvTaskDispatch> selectAgvTaskDispatchListByEndPoint(String endPoint) {
        return agvTaskDispatchMapper.selectAgvTaskDispatchListByEndPoint(endPoint);
    }

    @Override
    public
    List<AgvTaskDispatch> selectAgvTaskDispatchListByTrayNo(String trayNo){
        return agvTaskDispatchMapper.selectAgvTaskDispatchListByTrayNo(trayNo);
    }

    /**
     * 新增AGV任务调度
     *
     * @param agvTaskDispatch AGV任务调度
     * @return 结果
     */
    @Override
    public int insertAgvTaskDispatch(AgvTaskDispatch agvTaskDispatch) {
        agvTaskDispatch.setCreateTime(DateUtils.getNowDate());
        agvTaskDispatch.setCreateBy(SecurityUtils.getUsername());
        return agvTaskDispatchMapper.insertAgvTaskDispatch(agvTaskDispatch);
    }

    /**
     * 批量新增AGV任务调度
     *
     * @param agvTaskDispatchList AGV任务调度列表
     * @return 结果
     */
    @Override
    public int batchInsertAgvTaskDispatch(List<AgvTaskDispatch> agvTaskDispatchList) {
        Date currerTime = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for (AgvTaskDispatch agvTaskDispatch : agvTaskDispatchList){
            agvTaskDispatch.setCreateTime(currerTime);
            agvTaskDispatch.setCreateBy(username);
        }
        int insertNum = 0;
        for (int i=0; i<agvTaskDispatchList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskDispatchList.size()) {
                endIndex = agvTaskDispatchList.size();
            }
            insertNum = insertNum + agvTaskDispatchMapper.batchInsertAgvTaskDispatch(agvTaskDispatchList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改AGV任务调度
     *
     * @param agvTaskDispatch AGV任务调度
     * @return 结果
     */
    @Override
    public int updateAgvTaskDispatch(AgvTaskDispatch agvTaskDispatch) {
        try {
            agvTaskDispatch.setUpdateBy(SecurityUtils.getUsername());
        } catch (Exception e) {
            agvTaskDispatch.setUpdateBy("RCS");
        }
        agvTaskDispatch.setUpdateTime(DateUtils.getNowDate());
        return agvTaskDispatchMapper.updateAgvTaskDispatch(agvTaskDispatch);
    }

    /**
     * 批量修改AGV任务调度
     *
     * @param agvTaskDispatchList AGV任务调度列表
     * @return 结果
     */
    @Override
    public int batchUpdateAgvTaskDispatch(List<AgvTaskDispatch> agvTaskDispatchList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AgvTaskDispatch agvTaskDispatch : agvTaskDispatchList){
            agvTaskDispatch.setUpdateBy(username);
            agvTaskDispatch.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<agvTaskDispatchList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskDispatchList.size()) {
                endIndex = agvTaskDispatchList.size();
            }
            updateNum = updateNum + agvTaskDispatchMapper.batchUpdateAgvTaskDispatch(agvTaskDispatchList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除AGV任务调度
     *
     * @param ids 需要删除的AGV任务调度主键集合
     * @return 结果
     */
    @Override
    public int deleteAgvTaskDispatchByIds(Long[] ids) {
        return agvTaskDispatchMapper.deleteAgvTaskDispatchByIds(ids);
    }

    /**
     * 删除AGV任务调度信息
     *
     * @param id AGV任务调度主键
     * @return 结果
     */
    @Override
    public int deleteAgvTaskDispatchById(Long id) {
        return agvTaskDispatchMapper.deleteAgvTaskDispatchById(id);
    }

    /**
     * AGV任务回调
     *
     * @param vo 任务回调实体
     * @return AgvDispatchResponse
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgvResultVo agvCallback(AgvTaskCallbackVo vo) {
        AgvResultVo response = new AgvResultVo();
        AgvTaskDispatch taskDispatch=taskDispatchService.getTaskByNo(vo.getReceiveTaskID());
        if (taskDispatch == null) {
            response.setCode(AgvResultCodeEnum.Task_No_Exist.getCode());
            response.setMsg("任务编号不存在");
            log.error("任务编号不存在");
            return response;
        }
        if (StringUtils.equals(taskDispatch.getTaskStatus(), AgvEnum.AGV_TASK_STATUS.END.getCode())) {
            response.setCode(AgvResultCodeEnum.Task_No_Exist.getCode());
            log.error("任务已经执行结束，不能重新执行");
            response.setMsg("任务已经执行结束，不能重新执行");
            return response;
        }
        response.setCode(AgvResultCodeEnum.Success.getCode());
        response.setSuccess(true);
        response.setMsg("");
        AgvRcsRecord rcsRecord = new AgvRcsRecord();
        rcsRecord.setSend("RCS");
        rcsRecord.setReceive("WCS");
        log.info("AGV回调开始" + vo.toString());

        // 任务开始
        if (vo.getMethod().equals(AgvEnum.AGV_TASK_STATUS.START.getCode())) {
            log.info("[START]数据：{}", vo);
            rcsRecord.setMethod(AgvEnum.AGV_TASK_STATUS.START.getCode());
            setRcsRecord(vo, response, rcsRecord);
            AgvTaskDispatch entity = getAgvTaskDispatch(vo.getReceiveTaskID());
            if (null != entity) {
                // 如果不是已发送状态，直接跳过
//                if (!entity.getTaskStatus().equals(AgvEnum.AGV_TASK_STATUS.SENT.getCode())) {
//                    return response;
//                }

                // 更新RCS任务状态
                entity.setAgvCode(vo.getMachineCode());
                entity.setTaskStatus(AgvEnum.AGV_TASK_STATUS.START.getCode());
                taskDispatchService.updateAgvTaskDispatch(entity);
                // 记录日志
                setTaskDisRecord(entity.getTaskCode(), "agv开始任务", AgvEnum.AGV_TASK_STATUS.START.getCode());
            }
        }
        // 任务结束
        if (vo.getMethod().equals(AgvEnum.AGV_TASK_STATUS.END.getCode())) {
            //记录
            rcsRecord.setMethod(AgvEnum.AGV_TASK_STATUS.END.getCode());
            setRcsRecord(vo, response, rcsRecord);
            log.info("[END]数据：{}", vo);
            AgvTaskDispatch entity = getAgvTaskDispatch(vo.getReceiveTaskID());
            if (null != entity) {
                entity.setTaskStatus(AgvEnum.AGV_TASK_STATUS.END.getCode());
                taskDispatchService.updateAgvTaskDispatch(entity);

                // 记录日志
                setTaskDisRecord(entity.getTaskCode(), "agv任务调度完成成功", AgvEnum.AGV_TASK_STATUS.END.getCode());
                // 调用agv移库业务，更改目标库位，生成库存流水
                wmsStorehouseStockService.handleAgvEndEvent(entity.getStartLocationCode(),entity.getEndLocationCode(),taskDispatch);
            }
        }
        // 走出储位,释放起点
        if (vo.getMethod().equals(AgvEnum.AGV_TASK_STATUS.OUTBIN.getCode())) {
            log.info("[OUTBIN]数据：{}", vo);
            rcsRecord.setMethod(AgvEnum.AGV_TASK_STATUS.OUTBIN.getCode());
            setRcsRecord(vo, response, rcsRecord);
            AgvTaskDispatch entity = getAgvTaskDispatch(vo.getReceiveTaskID());
            if (null != entity && !StringUtils.equals(entity.getTaskStatus(), AgvEnum.AGV_TASK_STATUS.OUTBIN.getCode())) {
                entity.setTaskStatus(AgvEnum.AGV_TASK_STATUS.OUTBIN.getCode());
                taskDispatchService.updateAgvTaskDispatch(entity);
                //释放起点
                wmsStorehouseStockService.handleOutBinEvent(entity.getStartLocationCode(), entity);
                // 记录日志
                setTaskDisRecord(entity.getTaskCode(), "agv任务调度完成成功", AgvEnum.AGV_TASK_STATUS.OUTBIN.getCode());
            }
        }

        return response;
    }


    /**
     * 回调 rcs记录
     *
     * @param vo 数据
     * @param response 结果
     * @param rcsRecord record对象
     */
    private void setRcsRecord(AgvTaskCallbackVo vo, AgvResultVo response, AgvRcsRecord rcsRecord) {
        rcsRecord.setParam(new JSONObject(vo).toString());
        rcsRecord.setTaskCode(vo.getReceiveTaskID());
        rcsRecord.setResponse(response.toString());
        rcsRecord.setStatus(AgvEnum.SYN_STATUS.SUCCESS.getCode());
        rcsRecordService.insertAgvRcsRecord(rcsRecord);
    }


    /**
     * 根绝任务code获取 agv任务
     *
     * @param taskCode 任务编号
     * @return agv任务对象
     */
    private AgvTaskDispatch getAgvTaskDispatch(String taskCode) {
        AgvTaskDispatch queryParam = new AgvTaskDispatch();
        queryParam.setTaskCode(taskCode);
        List<AgvTaskDispatch> list = this.agvTaskDispatchMapper.selectAgvTaskDispatchList(queryParam);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 记录任务调度 record
     *
     * @param taskCode 任务编号
     * @param reason 原因
     * @param taskStatus 状态
     */
    private void setTaskDisRecord(String taskCode, String reason, String taskStatus) {
        AgvTaskDispatchRecord record = new AgvTaskDispatchRecord();
        record.setReason(reason);
        record.setTaskCode(taskCode);
        record.setTaskStatus(taskStatus);
        taskDispatchRecordService.insertAgvTaskDispatchRecord(record);
    }

    /**
     * 发送agv指令给小车
     *
     * @param vo vo
     */
    @Override
    public boolean sendAgvSchedulingTask(AgvCreateTaskVo vo) {
        if (!vo.getTaskStatus().equals(AgvEnum.AGV_TASK_STATUS.SENDING.getCode())) {
            log.error("任务状态不正确,任务状态：{}，任务：{}，点位：{}",vo.getTaskStatus(), vo.getReceiveTaskID(), vo.getPriority());
            return false;
        }
        AgvRcsRecord rcsRecord = getAgvRcsRecord(new JSONObject(vo).toString(),vo.getReceiveTaskID(), SCHEDULING_TASK);
        Gson gson = new Gson();
        log.info("agv生成任务单开始" + new JSONObject(vo).toString());
        R fail = R.fail("发送agv任务调度失败");
        try {
            String agvUrl1 = sysconfig.selectConfigByKey("agvUrl");
            if (!agvUrl1.isEmpty()) {
                agvUrl = agvUrl1 ;
            }
            JSONObject jsonObject = new JSONObject(vo);

            String result = HttpUtil.post(agvUrl + SCHEDULING_TASK, JSONUtil.toJsonStr(jsonObject));
            log.info("agv生成任务单结束" + result);
            AgvResultVo response = gson.fromJson(result, AgvResultVo.class);
            // 调用成功插入任务调度数据
            if (response.getSuccess()) {
                //自己记录每次发送agv任务成功，记录rcs随机的taskCode
                setTaskDisRecord(vo.getReceiveTaskID(), "发送agv任务成功", AgvEnum.AGV_TASK_STATUS.SENT.getCode());
                //rcs 成功记录的每次返回消息
                rcsRecord.setResponse(response.toString());
                rcsRecord.setStatus(AgvEnum.SYN_STATUS.SUCCESS.getCode());
                rcsRecordService.insertAgvRcsRecord(rcsRecord);
                return true;
            } else {
                rcsRecordService.setRcsFailRecord(rcsRecord, fail, new RuntimeException("发送agv任务调度失败"));
                log.error("发送agv任务调度失败");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            rcsRecordService.setRcsFailRecord(rcsRecord, fail, e);
            log.error(e.getMessage());
            return false;
        }

    }

    /**
     * 发送agv指令给小车
     *
     * @param taskVoList taskVoList
     */
    private boolean sendAgvSchedulingTasks(List<AgvCreateTaskVo> taskVoList) {
        Gson gson = new Gson();
        log.info("agv生成任务单开始" + new JSONObject(taskVoList).toString());
        R fail = R.fail("发送agv任务调度失败");
        try {
            String agvUrl1 = sysconfig.selectConfigByKey("agvUrl");
            if (!agvUrl1.isEmpty()) {
                agvUrl = agvUrl1;
            }
            JSONArray jsonArray = new JSONArray(taskVoList);
            String result = HttpUtil.post(agvUrl + SCHEDULING_TASK_LIST, JSONUtil.toJsonStr(jsonArray));
            log.info("agv生成任务单结束" + result);
            AgvRcsRecord rcsRecord = getAgvRcsRecord(new JSONArray(taskVoList).toString(), "批量任务", SCHEDULING_TASK_LIST);
            AgvResultDataListVo response = gson.fromJson(result, AgvResultDataListVo.class);
            if (ObjectUtil.isEmpty(response) || CollUtil.isEmpty(response.getDataList())) {
                log.error("发送agv任务调度失败");
                return false;
            } else {
                for (AgvResultVo agvResultVo : response.getDataList()) {
                    if (0 == agvResultVo.getCode()) {
                        //自己记录每次发送agv任务成功，记录rcs随机的taskCode
                        setTaskDisRecord(agvResultVo.getContent(), "发送agv任务成功", AgvEnum.AGV_TASK_STATUS.SENT.getCode());
                        //rcs 成功记录的每次返回消息
                    } else {
                        rcsRecordService.setRcsFailRecord(rcsRecord, fail, new RuntimeException("发送agv任务调度失败"));
                        log.error("发送agv任务调度失败");
                        continue;
                    }
                }
                rcsRecord.setResponse(response.toString());
                rcsRecord.setStatus(AgvEnum.SYN_STATUS.SUCCESS.getCode());
                rcsRecordService.insertAgvRcsRecord(rcsRecord);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 记录rcs通讯信息
     *
     * @param * @Param null:
     * @return 纪录对象
     */
    private AgvRcsRecord getAgvRcsRecord(String param, String taskCode,String method) {
        AgvRcsRecord rcsRecord = new AgvRcsRecord();
        rcsRecord.setMethod(method);
        rcsRecord.setParam(param);
        rcsRecord.setTaskCode(taskCode);
        rcsRecord.setSend("WCS");
        rcsRecord.setReceive("RCS");
        String agvUrl1 = sysconfig.selectConfigByKey("agvUrl");
        if (!agvUrl1.isEmpty()) {
            agvUrl = agvUrl1 ;
        }
        rcsRecord.setAgvUrl(agvUrl+"/"+method);
        rcsRecord.setCreateBy("admin");
        return rcsRecord;
    }

    /**
     * 取消任务
     *
     * @param id 任务id
     * @return com.canaan.common.core.util.R
     */
    @Override
    public R cancelTask(Long id) {
        AgvTaskDispatch entity = taskDispatchService.selectAgvTaskDispatchById(id);
        String taskStatus = entity.getTaskStatus();
        if(flag) {
            if (taskStatus.equals(AgvEnum.AGV_TASK_STATUS.SENT.getCode())
                    || taskStatus.equals(AgvEnum.AGV_TASK_STATUS.START.getCode())) {
                AgvStopTaskVo vo = new AgvStopTaskVo();
                vo.setReceiveTaskID(entity.getTaskCode());
                AgvRcsRecord rcsRecord = getAgvRcsRecord(new JSONObject(vo).toString(), vo.getReceiveTaskID(),CANCEL_TASK);
                R fail = R.fail("取消agv任务调度失败");
                log.info("取消agv任务开始" + vo.toString());
                try {
                    String agvUrl1 = sysconfig.selectConfigByKey("agvUrl");
                    if (!agvUrl1.isEmpty()) {
                        agvUrl = agvUrl1 ;
                    }
                    String result = HttpUtil.post( agvUrl + CANCEL_TASK, new JSONObject(vo));

                    Gson gson = new Gson();
                    AgvResultVo response = gson.fromJson(result, AgvResultVo.class);
                    log.info("取消agv任务结束" + response.toString());
                    if (0!=response.getCode()) {
                        setTaskDisRecord(entity.getTaskCode(), "取消agv任务失败", taskStatus);
                        rcsRecord.setResponse(response.toString());
                        String cancelTaskNotLine = "任务单" + entity.getTaskCode() + "已结束或已取消";
                        if (cancelTaskNotLine.equalsIgnoreCase(response.getMsg())) {
                            log.info("小车任务不存在或执行成功");
                            rcsRecord.setStatus(AgvEnum.SYN_STATUS.SUCCESS.getCode());
                            rcsRecordService.insertAgvRcsRecord(rcsRecord);
                            return fail;
                        } else {
                            rcsRecord.setStatus(AgvEnum.SYN_STATUS.FAIL.getCode());
                            rcsRecordService.insertAgvRcsRecord(rcsRecord);
                            return fail;
                        }
                    } else {
                        //rcs 成功记录
                        rcsRecord.setResponse(response.toString());
                        rcsRecord.setStatus(AgvEnum.SYN_STATUS.SUCCESS.getCode());
                        rcsRecordService.insertAgvRcsRecord(rcsRecord);

                        // 调用成功插入任务调度数据
                        return cancelTaskDispatch(entity, "agv任务调度取消成功");
                    }
                } catch (Exception e) {
                    rcsRecordService.setRcsFailRecord(rcsRecord, fail, e);
                    return fail;
                }
            }
            else{
                return R.fail("任务"+entity.getTaskCode()+"状态不可以取消");
            }

        }
        else {
            return cancelTaskDispatch(entity, "agv任务调度取消成功");
        }
    }

    /**
     * 根据任务编号查询任务
     *
     * @param no 任务no
     * @return com.canaan.common.core.util.R
     */
    @Override
    public AgvTaskDispatch getTaskByNo(String no) {
        if (StringUtils.isEmpty(no)) {
            return null;
        }
        AgvTaskDispatch queryParam = new AgvTaskDispatch();
        queryParam.setTaskCode(no);
        List<AgvTaskDispatch> list = taskDispatchService.selectAgvTaskDispatchList(queryParam);
        if (CollUtil.isEmpty(list)) {
            throw new ServiceException("AGV任务数据不存在");
        }
        return list.get(0);
    }
    /**
     * 根据任务编号取消任务
     *
     * @param no 任务no
     * @return com.canaan.common.core.util.R
     */
    @Override
    public R cancelTaskByNo(String no) {
        return cancelTask(getTaskByNo(no).getId());
    }

    /**
     * 取消agv任务成功并记录在 任务调度表
     *
     * @param entity 任务对象
     * @return
     */
    private R cancelTaskDispatch(AgvTaskDispatch entity, String reason) {
        entity.setTaskStatus(AgvEnum.AGV_TASK_STATUS.CANCEL.getCode());
        taskDispatchService.updateAgvTaskDispatch(entity);
        WmsStorehouseStock sourceStock=wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(entity.getStartLocationCode());
        WmsStorehouseStock destStock=wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(entity.getEndLocationCode());
        if(sourceStock!=null&& StorehouseStockStatusEnum.LOCK.getCode().equals(sourceStock.getStatus())){
            sourceStock.setStatus(StorehouseStockStatusEnum.NORMAL.getCode());
            wmsStorehouseStockService.updateWmsStorehouseStock(sourceStock);
        }
        if(destStock!=null&&StorehouseStockStatusEnum.LOCK.getCode().equals(sourceStock.getStatus())){
            destStock.setStatus(StorehouseStockStatusEnum.NORMAL.getCode());
            wmsStorehouseStockService.updateWmsStorehouseStock(destStock);
        }
        setTaskDisRecord(entity.getTaskCode(), reason, AgvEnum.AGV_TASK_STATUS.CANCEL.getCode());
        return R.ok(true);
    }
    /**
     * 根据taskDispatch发送小车指令
     *
     * @param sendDispatch 任务调度数据
     */
    @Override
    public synchronized boolean sendSchedulingTaskByAgvDispatch(AgvTaskDispatch sendDispatch) {
        if(checkLocation(sendDispatch)){
            //此不执行
            return false;
        }
        log.info("[异步的下发AGV同步方法]");
        AgvCreateTaskVo createTaskVo=new AgvCreateTaskVo();
        String sysToken1 = sysconfig.selectConfigByKey("sysToken");
        if (!sysToken1.isEmpty()) {
            sysToken = sysToken1 ;
        }else{
            log.error("sysToken 未在数据库配置");
        }
        createTaskVo.setSysToken(sysToken);
        String mapCode1 = sysconfig.selectConfigByKey("mapCode");
        if (!mapCode1.isEmpty()) {
            mapCode = mapCode1 ;
        }else{
            log.error("mapCode 未在数据库配置");
        }
        createTaskVo.setMapCode(mapCode);
        createTaskVo.setReceiveTaskID(sendDispatch.getTaskCode());
        createTaskVo.setTaskCode(sendDispatch.getTemplateCode());
        createTaskVo.setPriority(0);
        createTaskVo.setTaskStatus(sendDispatch.getTaskStatus());
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<AgvPositionCodeVO>>() {
        }.getType();
        ArrayList<AgvPositionCodeVO> list = gson.fromJson(sendDispatch.getPositionCodePath(), type);
        AgvVariableVo[] agvVariableVos=new AgvVariableVo[list.size()];
        for(int i=0;i< list.size();i++){
            AgvPositionCodeVO item=list.get(i);
            AgvVariableVo agvVariableVo=new AgvVariableVo();
            if(i==0) {
                agvVariableVo.setCode("StartPoint");
            }
            else if(i==list.size()-1){
                agvVariableVo.setCode("EndPoint");
            }
            else{
                agvVariableVo.setCode("MidPoint");
            }
            agvVariableVo.setValue(item.getPositionCode());
            agvVariableVos[i]=agvVariableVo;
        }
        createTaskVo.setVariables(agvVariableVos);
        boolean result = this.sendAgvSchedulingTask(createTaskVo);
        if (result) {
            // 发送成功，更新任务的状态为SENT
            sendDispatch.setTaskStatus(AgvEnum.AGV_TASK_STATUS.SENT.getCode());
            this.updateAgvTaskDispatch(sendDispatch);
        }
        return result;
    }

    private boolean checkLocation(AgvTaskDispatch sendDispatch) {
        WmsStorehouseStock endStock = wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(sendDispatch.getEndPosition());
        if (StrUtil.isNotEmpty(endStock.getContainerNo())) {
            log.error("终点对应库有料架，任务：{}，点位：{}", sendDispatch.getTaskCode(), sendDispatch.getEndPosition());
            return true;     //终点对应库有料架，不执行本任务
        }

        List<AgvTaskDispatch> startTasks = agvTaskDispatchMapper.selectAgvTaskDispatchListByStartPoint(sendDispatch.getStartPosition());
        if (CollectionUtil.isNotEmpty(startTasks)) {
            for (AgvTaskDispatch obj : startTasks) {
                if (!StringUtils.equals(obj.getTaskCode(), sendDispatch.getTaskCode())) {
                    log.error("起点存在相同终点正在执行任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), sendDispatch.getStartPosition());
                    return true;
                }
            }
        }

        List<AgvTaskDispatch> endTasks = agvTaskDispatchMapper.selectAgvTaskDispatchListByEndPoint(sendDispatch.getEndPosition());
        if (CollectionUtil.isNotEmpty(endTasks)) {
            for (AgvTaskDispatch obj : endTasks) {
                if (!StringUtils.equals(obj.getTaskCode(), sendDispatch.getTaskCode())) {
                    log.error("终点存在相同终点正在执行任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), sendDispatch.getEndPosition());
                    return true;
                }
            }
        }
        //起点判断是否里库,如果是里库,判断是否有起点在外库的任务,如果有,不执行本任务
        WmsStorehouseLocation startPoint = locationService.selectWmsStorehouseLocationByNo(sendDispatch.getStartPosition());
        if (StringUtils.equalsIgnoreCase(startPoint.getIsInner(), "1")) {
            int num = agvTaskDispatchMapper.queryRunningTaskByStartPoint(startPoint.getInnerLocation());
            if (num > 0) {
                log.error("起点对应外库存在任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), startPoint.getAgvPositionCode());
                return true;
            }
            //TODO 应判断外库是否有料架
            WmsStorehouseStock wmsStorehouseStock = wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(startPoint.getInnerLocation());
            if (StrUtil.isNotEmpty(wmsStorehouseStock.getContainerNo())) {
                log.error("起点对应外库有料架，任务：{}，点位：{}", sendDispatch.getTaskCode(), startPoint.getAgvPositionCode());
                //TODO 新增调库单
                emptyStockHelper.generateAdjustLocation(startPoint.getNo(),startPoint.getIsInner(),startPoint.getInnerLocation(),null);
                log.error("调库单生成成功，任务：{}，点位：{}", sendDispatch.getTaskCode(), startPoint.getInnerLocation());
                return true;     //起点对应外库有料架，不执行本任务
            }
        }
        //终点判断是否外库,如果是外库,判断是否有终点在里库的任务,如果有,不执行本任务,判断是否有起点在里库的任务,如果有,不执行本任务
        WmsStorehouseLocation endPoint = locationService.selectWmsStorehouseLocationByNo(sendDispatch.getEndPosition());
        if (StringUtils.equalsIgnoreCase(endPoint.getIsInner(), "0")) {
            //没有内库
            if (StringUtils.isEmpty(endPoint.getInnerLocation())) {
                return false;
            }
            int num = agvTaskDispatchMapper.queryRunningTaskByEndPoint(endPoint.getInnerLocation());
            if (num > 0) {
                log.error("终点对应里库存在入库任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), startPoint.getAgvPositionCode());
                return true;
            }
            num = agvTaskDispatchMapper.queryRunningTaskByStartPoint(endPoint.getInnerLocation());
            if (num > 0) {
                log.error("终点对应里库存在出库任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), startPoint.getAgvPositionCode());
                return true;
            }
            //TODO 应判断里库是否有料架
            WmsStorehouseStock wmsStorehouseStock = wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(endPoint.getInnerLocation());
            if (StrUtil.isEmpty(wmsStorehouseStock.getContainerNo())) {
                log.error("终点对应内库无料架，任务：{}，点位：{}", sendDispatch.getTaskCode(), endPoint.getAgvPositionCode());
                return true;     //终点对应外库无料架，不执行本任务
            }
            if (StringUtils.equals(wmsStorehouseStock.getStatus(), StorehouseStockStatusEnum.LOCK.getCode())) {
                log.error("终点对应内库被锁定，任务：{}，点位：{}", sendDispatch.getTaskCode(), endPoint.getAgvPositionCode());
                return true;     //终点对应外库无料架，不执行本任务
            }

        }
//        if (StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.BACK.getCode())
//                || StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.IN.getCode())
//                || StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.ADJUST.getCode())) {
//            WmsStorehouseLocation endPoint = locationService.selectWmsStorehouseLocationByNo(sendDispatch.getEndPosition());
//            if (StringUtils.equalsIgnoreCase(endPoint.getIsInner(), "0")) {
//                //外库入，判断内库点位是否有（非END）任务，有的话，本任务不执行
//                int num = agvTaskDispatchMapper.queryRunningTaskByPoint(endPoint.getInnerLocation());
//                if (num > 0) {
//                    log.error("点位对应里库存在任务，任务：{}，点位：{}", sendDispatch.getTaskCode(), endPoint.getAgvPositionCode());
//                    return true;
//                }
//            }
//        } else if (StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.OUT.getCode())
//                || StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.ADJUST.getCode())
//                || StringUtils.equalsIgnoreCase(sendDispatch.getTaskType(), AgvEnum.TASK_TYPE.APPLY_EMPTY.getCode())) {
//            WmsStorehouseLocation startPoint = locationService.selectWmsStorehouseLocationByNo(sendDispatch.getStartPosition());
//            if (StringUtils.equalsIgnoreCase(startPoint.getIsInner(), "1")) {
//                //里库出，判断外库有没有出库的（非OUTBIN、END）任务，有的话，本任务不执行
//                int num = agvTaskDispatchMapper.queryOutTaskByPoint(startPoint.getInnerLocation(), AgvEnum.TASK_TYPE.OUT.getCode());
//                if (num > 0) {
//                    log.error("点位对应外库存在空车任务，任务：{}，点位：{}", startPoint.getInnerLocation(), sendDispatch.getEndPosition());
//                    return true;
//                }
//                num = agvTaskDispatchMapper.queryOutTaskByPoint(startPoint.getInnerLocation(), AgvEnum.TASK_TYPE.APPLY_EMPTY.getCode());
//                if (num > 0) {
//                    log.error("点位对应外库存在空料架任务，任务：{}，点位：{}", startPoint.getInnerLocation(), sendDispatch.getEndPosition());
//                    return true;
//                }
//                num = agvTaskDispatchMapper.queryOutTaskByPoint(startPoint.getInnerLocation(), AgvEnum.TASK_TYPE.ADJUST.getCode());
//                if (num > 0) {
//                    log.error("点位对应外库存在调库任务，任务：{}，点位：{}", startPoint.getInnerLocation(), sendDispatch.getEndPosition());
//                    return true;
//                }
//            }
//        }
        return false;
    }

    @Override
    public boolean sendRcsTaskList(List<AgvTaskDispatch> dispatchList) {
        if (CollUtil.isEmpty(dispatchList)) {
            return false;
        }
        List<AgvCreateTaskVo> taskVos = new ArrayList<>();
        for (AgvTaskDispatch dispatch : dispatchList) {
            taskVos.add(createAgvTaskVo(dispatch));
        }
        sendAgvSchedulingTasks(taskVos);
        return true;
    }

    private AgvCreateTaskVo createAgvTaskVo(AgvTaskDispatch sendDispatch) {
        AgvCreateTaskVo createTaskVo = new AgvCreateTaskVo();
        String sysToken1 = sysconfig.selectConfigByKey("sysToken");
        if (!sysToken1.isEmpty()) {
            sysToken = sysToken1;
        } else {
            log.error("sysToken 未在数据库配置");
        }
        createTaskVo.setSysToken(sysToken);
        String mapCode1 = sysconfig.selectConfigByKey("mapCode");
        if (!mapCode1.isEmpty()) {
            mapCode = mapCode1;
        } else {
            log.error("mapCode 未在数据库配置");
        }
        createTaskVo.setMapCode(mapCode);
        createTaskVo.setReceiveTaskID(sendDispatch.getTaskCode());
        createTaskVo.setTaskCode(sendDispatch.getTemplateCode());
        createTaskVo.setPriority(0);
        createTaskVo.setTaskStatus(sendDispatch.getTaskStatus());
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<AgvPositionCodeVO>>() {
        }.getType();
        ArrayList<AgvPositionCodeVO> list = gson.fromJson(sendDispatch.getPositionCodePath(), type);
        AgvVariableVo[] agvVariableVos = new AgvVariableVo[list.size()];
        for (int i = 0; i < list.size(); i++) {
            AgvPositionCodeVO item = list.get(i);
            AgvVariableVo agvVariableVo = new AgvVariableVo();
            if (i == 0) {
                agvVariableVo.setCode("StartPoint");
            } else if (i == list.size() - 1) {
                agvVariableVo.setCode("EndPoint");
            } else {
                agvVariableVo.setCode("MidPoint");
            }
            agvVariableVo.setValue(item.getPositionCode());
            agvVariableVos[i] = agvVariableVo;
        }
        createTaskVo.setVariables(agvVariableVos);

        return createTaskVo;
    }

    /**
     * 创建AGV任务
     * @param billNo 单据号
     * @param source 源库位
     * @param dest 目标库位
     * @return true-成功,false-失败
     */
    @Override
    public void createTaskDispatch(String billNo, WmsStorehouseLocation source, WmsStorehouseLocation dest, AgvEnum.TASK_TYPE taskType,String shelfNo,String trayNo){
        AgvTaskDispatch agvTaskDispatch=new AgvTaskDispatch();
        agvTaskDispatch.setTaskCode(UUID.randomUUID().toString());
        agvTaskDispatch.setEndAreaCode(dest.getStorehouseAreaNo());
        agvTaskDispatch.setEndLocationCode(dest.getNo());
        agvTaskDispatch.setEndPosition(dest.getAgvPositionCode());
        agvTaskDispatch.setEndStorehouseCode(dest.getStorehouseNo());
        agvTaskDispatch.setStartLocationCode(source.getNo());
        agvTaskDispatch.setStartPosition(source.getAgvPositionCode());
        agvTaskDispatch.setStartStorehouseCode(source.getStorehouseNo());
        agvTaskDispatch.setStartAreaCode(source.getStorehouseAreaNo());
        agvTaskDispatch.setTaskType(taskType.getCode());
        agvTaskDispatch.setTaskStatus(AgvEnum.AGV_TASK_STATUS.SENDING.getCode());
        agvTaskDispatch.setRefBillNo(billNo);
        agvTaskDispatch.setShelfNo(shelfNo);
        agvTaskDispatch.setTrayNo(trayNo);
        AgvTaskConfig taskConfig=getAgvTaskConfig(source.getStorehouseAreaNo(),dest.getStorehouseAreaNo());
        if(taskConfig==null){
            throw new RuntimeException("不存在模板配置数据");
        }
        if(StringUtils.isEmpty(source.getAgvPositionCode())){
            throw new RuntimeException("库位"+source.getNo()+"不存在agv点位数据");
        }

        if(StringUtils.isEmpty(dest.getAgvPositionCode())){
            throw new RuntimeException("库位"+dest.getNo()+"不存在agv点位数据");
        }
        AgvPositionCodeVO[] positionPaths=new AgvPositionCodeVO[2];
        positionPaths[0]=new AgvPositionCodeVO();
        positionPaths[0].setPositionCode(source.getAgvPositionCode());
        positionPaths[0].setType("1");
        positionPaths[1]=new AgvPositionCodeVO();
        positionPaths[1].setPositionCode(dest.getAgvPositionCode());
        positionPaths[1].setType("1");
        Gson gson = new Gson();
        agvTaskDispatch.setPositionCodePath(gson.toJson(positionPaths));
        agvTaskDispatch.setTemplateCode(taskConfig.getTemplateCode());
        agvTaskDispatch.setCreateBy(SecurityUtils.getUsername());
        agvTaskDispatch.setCreateTime(DateUtils.getNowDate());
        agvTaskDispatchMapper.insertAgvTaskDispatch(agvTaskDispatch);
        //锁定起点、终点
        WmsStorehouseLocation startPoint = locationService.selectWmsStorehouseLocationByAgvPoint(source.getAgvPositionCode());
        if (ObjectUtil.isNotEmpty(startPoint)) {
            startPoint.setStatus(AgvEnum.STOREHOUSE_STATUS.LOCK.getCode());
            locationService.updateWmsStorehouseLocation(startPoint);
        }
        WmsStorehouseLocation endPoint = locationService.selectWmsStorehouseLocationByAgvPoint(dest.getAgvPositionCode());
        if (ObjectUtil.isNotEmpty(endPoint)) {
            endPoint.setStatus(AgvEnum.STOREHOUSE_STATUS.LOCK.getCode());
            locationService.updateWmsStorehouseLocation(endPoint);
        }
    }

    @Override
    public AgvResultVo finishAgvCallback(BillAgvVo vo) {
        AgvTaskCallbackVo agvTaskCallbackVo = new AgvTaskCallbackVo();
        AgvTaskDispatch agvTaskDispatch = new AgvTaskDispatch();
        agvTaskDispatch.setRefBillNo(vo.getNo());
        agvTaskDispatch.setDelFlag(0);
        List<AgvTaskDispatch> agvTaskDispatches = agvTaskDispatchMapper.selectAgvTaskDispatchList(agvTaskDispatch);
        if (!CollUtil.isEmpty(agvTaskDispatches)) {
            //先离开,清空起点库存
            agvTaskCallbackVo.setReceiveTaskID(agvTaskDispatches.get(0).getTaskCode());
            agvTaskCallbackVo.setMethod("OUTBIN");
            agvCallback(agvTaskCallbackVo);
            //在结束,绑定终点库存
            agvTaskCallbackVo.setMethod(vo.getMethod());
            return agvCallback(agvTaskCallbackVo);

        }
//        if (StringUtils.equalsIgnoreCase(AgvEnum.IN_OUT.IN.getCode(), vo.getTaskType())) {
//            InStockBill inStockBill = new InStockBill();
//            inStockBill.setRelOrderNo(vo.getNo());
//            List<InStockBill> list = inStockBillService.selectInStockBillList(inStockBill);
//            ;
//            if (CollUtil.isNotEmpty(list)) {
//                agvTaskCallbackVo.setReceiveTaskID(list.get(0).getNo());
//                agvTaskCallbackVo.setMethod(vo.getMethod());
//                return agvCallback(agvTaskCallbackVo);
//            }
//        } else if (StringUtils.equalsIgnoreCase(AgvEnum.IN_OUT.OUT.getCode(), vo.getTaskType())) {
//            OutStockBill outStockBill = new OutStockBill();
//            outStockBill.setRelOrderNo(vo.getNo());
//            List<OutStockBill> list = outStockBillService.selectOutStockBillList(outStockBill);
//            ;
//            if (CollUtil.isNotEmpty(list)) {
//                agvTaskCallbackVo.setReceiveTaskID(list.get(0).getNo());
//                agvTaskCallbackVo.setMethod(vo.getMethod());
//                return agvCallback(agvTaskCallbackVo);
//            }
//        }
        AgvResultVo response = new AgvResultVo();
        response.setCode(AgvResultCodeEnum.Task_No_Exist.getCode());
        response.setMsg("任务编号不存在");
        return response;
    }

    @Override
    public int queryRunningTaskByShelfNo(String containerNo) {
        return agvTaskDispatchMapper.queryRunningTaskByShelfNo(containerNo);
    }

    @Override
    public int queryRunningTaskByTrayNo(String trayNo) {
        return agvTaskDispatchMapper.queryRunningTaskByTrayNo(trayNo);
    }

    @Override
    public AjaxResult transferShelf(AgvTaskDispatch vo) {
        if(StringUtils.isEmpty(vo.getStartPosition())){
            return AjaxResult.error("起点不能为空");
        }
        if(StringUtils.isEmpty(vo.getEndPosition())){
            return AjaxResult.error("终点不能为空");
        }
        int count = agvTaskDispatchMapper.queryRunningTaskByPoint(vo.getStartPosition());
        if (count > 0) {
            return AjaxResult.error("起点存在任务");
        }
        count = agvTaskDispatchMapper.queryRunningTaskByPoint(vo.getEndPosition());
        if (count > 0) {
            return AjaxResult.error("终点存在任务");
        }
        WmsStorehouseStock startStock = wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(vo.getStartPosition());
        if (ObjectUtil.isEmpty(startStock) || StringUtils.isEmpty(startStock.getContainerNo())) {
            return AjaxResult.error("起点没有绑定货架");
        }
        if (StringUtils.equals(startStock.getStatus(), StorehouseStockStatusEnum.LOCK.getCode())) {
            return AjaxResult.error("起点被任务锁定");
        }
        WmsStorehouseLocation source = locationService.selectWmsStorehouseLocationByNo(vo.getStartPosition());
        if(ObjectUtil.isEmpty(source)){
            return AjaxResult.error("没有找到起点");
        }
        WmsStorehouseLocation dest = locationService.selectWmsStorehouseLocationByNo(vo.getEndPosition());
        if(ObjectUtil.isEmpty(dest)){
            return AjaxResult.error("没有找到终点");
        }
        WmsStorehouseStock destStock=wmsStorehouseStockService.selectWmsStorehouseStockByLocationNo(dest.getNo());
        if (ObjectUtil.isEmpty(startStock)){
            return AjaxResult.error("库位"+dest.getNo()+"库存数据不存在");
        }
        if (StringUtils.equals(startStock.getStatus(), StorehouseStockStatusEnum.LOCK.getCode())) {
            return AjaxResult.error("终点被任务锁定");
        }
        startStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
        wmsStorehouseStockService.updateWmsStorehouseStock(startStock);
        if(StorehouseStockStatusEnum.FREEZE.getCode().equals(startStock.getStatus())){
            destStock.setStatus(StorehouseStockStatusEnum.FREEZE.getCode());
        }
        else{
            destStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
        }
        wmsStorehouseStockService.updateWmsStorehouseStock(destStock);
        AdjustLocationBill adjustLocationBill = new AdjustLocationBill();
        adjustLocationBill.setShelfNo(startStock.getContainerNo());
        adjustLocationBill.setTrayNo(startStock.getTrayNo());
        adjustLocationBill.setSourceLocation(vo.getStartPosition());
        adjustLocationBill.setDestLocation(vo.getEndPosition());
        adjustLocationBill.setOrderStatus(InOrderStatusEnum.NEW.getCode());
        adjustLocationBillService.insertAdjustLocationBill(adjustLocationBill);
        createTaskDispatch(null, source, dest, AgvEnum.TASK_TYPE.ADJUST, startStock.getContainerNo(), startStock.getTrayNo());
        return AjaxResult.success("创建成功");
    }

    /**
     * 获取AGV模板
     * @param sourceArea 源库区
     * @param destArea 目标库区
     * @return AGV配置
     */
    private AgvTaskConfig getAgvTaskConfig(String sourceArea,String destArea){
        AgvTaskConfig query=new AgvTaskConfig();
        query.setStartAreaCode(sourceArea);
        query.setEndAreaCode(destArea);
        query.setDelFlag(0);
        List<AgvTaskConfig> agvTaskConfigList=agvTaskConfigService.selectAgvTaskConfigList(query);
        if(agvTaskConfigList!=null&&agvTaskConfigList.size()!=0){
            return agvTaskConfigList.get(0);
        }
        else{
            return null;
        }
    }
}
